//
//  TTSearchHistoryView.m
//  TaoTuo
//
//  Created by admin on 2019/11/27.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTSearchHistoryView.h"
#import "LLTagView.h"

@interface TTSearchHistoryView ()

@property (nonatomic, strong) LLTagView *historyTagView;

@end

@implementation TTSearchHistoryView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5;
        self.layer.borderColor = UIColorHex(E6E6E6).CGColor;
        self.layer.borderWidth = 1;
        
        [self setupSubviews];
        
    }
    return self;
}

///点击删除搜索记录
- (void)deleteBtnClick
{
    self.hidden = YES;
    [USER_DEFAULT removeObjectForKey:historySearchContentKey];
    [USER_DEFAULT synchronize];
}

/// 保存搜索历史
- (void)saveSearchHistoryWithText:(NSString *)text
{
    if (text.length == 0) {
        return;
    }
    NSMutableArray *historyArray = [[USER_DEFAULT objectForKey:historySearchContentKey] mutableCopy];
    if (historyArray.count == 0) {
        historyArray = [NSMutableArray array];
    }
    if ([historyArray containsObject:text]) {
        [historyArray removeObject:text];
    }
    [historyArray insertObject:text atIndex:0];
    [USER_DEFAULT setObject:historyArray forKey:historySearchContentKey];
    [USER_DEFAULT synchronize];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.height = 50 + self.historyTagView.height;
}

///刷新
- (void)reloadHistoryView
{
    // 历史搜索内容
    NSMutableArray *historyArray = [[USER_DEFAULT objectForKey:historySearchContentKey] mutableCopy];
    [self.historyTagView setupDatasArray:historyArray];
}


- (void)setupSubviews
{
    UILabel *historyLabel = [UILabel labelWithTitle:@"  历史搜索" color:UIColorHex(333333) fontSize:16];
    historyLabel.frame = CGRectMake(0, 0, 120, 40);
    [self addSubview:historyLabel];
    
    UIButton *deleteBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"lajitong"] target:self action:@selector(deleteBtnClick)];
    deleteBtn.imageView.contentMode = UIViewContentModeCenter;
    deleteBtn.frame = CGRectMake(self.width - 40, historyLabel.top, 40, 40);
    [self addSubview:deleteBtn];
    
    
    
    LLTagConfig *config = [[LLTagConfig alloc] init];
    config.minItemSpace = 5;
    config.minLineSpace = 5;
    config.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    config.itemHeight = 22;
    config.tagNormalbackgroundColor = UIColorHex(F0F4F7);
    config.tagSelectbackgroundColor = kMainColor;
    config.cornerRadius = 5;
    config.fontSize = 10;
    
    // 历史搜索内容
    NSMutableArray *historyArray = [[USER_DEFAULT objectForKey:historySearchContentKey] mutableCopy];
    @weakify(self)
    _historyTagView = [[LLTagView alloc] initWithFrame:CGRectMake(0, historyLabel.bottom, self.width, 50) dataArray:historyArray selectItems:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        [self saveSearchHistoryWithText:selectArray.firstObject];
        if (self.clickSearchHistoryBlock) {
            self.clickSearchHistoryBlock(selectArray.firstObject);
        }
        
    }];
    _historyTagView.needReloadView = self;
    _historyTagView.allowMutiSelect = NO;
    _historyTagView.backgroundColor = [UIColor whiteColor];
    _historyTagView.config = config;
    [self addSubview:_historyTagView];
    
}


@end
